package bootstrap.liftweb

import net.liftweb._
import util._
import Helpers._

import common._
import http._
import sitemap._
import Loc._
import _root_.net.liftweb.util._
import _root_.net.liftweb.common._
import net.liftweb.http._
import _root_.net.liftweb.http.provider._
import _root_.net.liftweb.sitemap._
import _root_.net.liftweb.sitemap.Loc._
import Helpers._
import _root_.net.liftweb.mapper.{DB, ConnectionManager, Schemifier, DefaultConnectionIdentifier, StandardDBVendor}
import _root_.java.sql.{Connection, DriverManager}
import _root_.edu.ucdavis.fiehnlab.alchemy.alchemyAdmin.model._
import net.liftweb.http.rest._
import collection.generic.SeqFactory
import net.liftweb.http.RewriteRequest
import net.liftweb.http.ParsePath
import net.liftweb.common.Full

import net.liftweb.http.rest.RestHelper
import edu.ucdavis.fiehnlab.alchemy.alchemyAdmin.snippet._

/**
 * A class that's instantiated early and run.  It allows the application
 * to modify lift's environment
 */
class Boot {
  def boot {
    if (!DB.jndiJdbcConnAvailable_?) {
      val vendor = 
	new StandardDBVendor(Props.get("db.driver") openOr "org.h2.Driver",
			     Props.get("db.url") openOr 
			     "jdbc:h2:lift_proto.db;AUTO_SERVER=TRUE",
			     Props.get("db.user"), Props.get("db.password"))

      LiftRules.unloadHooks.append(vendor.closeAllConnections_! _)

      DB.defineConnectionManager(DefaultConnectionIdentifier, vendor)
    }

    // where to search snippet
    LiftRules.addToPackages("edu.ucdavis.fiehnlab.alchemy.alchemyAdmin")
    Schemifier.schemify(true, Schemifier.infoF _, User,Method,Compound)
  /**
    // Build SiteMap
    def sitemap() = SiteMap(
      Menu("Home") / "index" :: // Simple menu form
      // Menu with special Link
      Menu(Loc("Static", Link(List("static"), true, "/static/index"), 
	       "Static Content")) ::

        /**
         * create new Method forum link
         */
        Menu(Loc("addMethod", Link(List("addMethod"),true,"/method/"),"Add New Method")) ::

        /**
         * add new compound Link
         */
        Menu(Loc("addCompound", Link(List("addCompound"),true,"/addCompound"),"Add New compound")) ::

        /**
         *
         * crud implementation
         */

       // Menu(Loc("Method",Link(List("method"),true,"/method/create"),"create"))::
        Menu(Loc("Method",Link(List("method"),true,"/method/list"),"list"))::
       // Menu(Loc("Method",Link(List("method"),true,"/method/view"),"view"))::
        Menu(Loc("Compound",Link(List("compound"),true,"/compound/create"),"addCompound"))::
      // Menu entries for the User management stuff
      User.sitemap :_*)

    LiftRules.setSiteMapFunc(sitemap)
       **/

  val entries = Menu(Loc("Home", List("index"), "Home")) ::
    Menu(Loc("Method", List("method") -> true, "Method", Hidden)) ::
    Menu(Loc("MethodList", List("method", "list"), "Method List")) ::
    Menu(Loc("MethodCreate", List("method", "create"), "Method Create")) ::
    Menu(Loc("Compound", List("compound") -> true, "Compound", Hidden)) ::
    Menu(Loc("CompoundList", List("compound","list"), "list Compound")) ::
    Menu(Loc("CompoundCreate", List("compound","create"), "add Compound"))::
    User.sitemap
    LiftRules.setSiteMap(SiteMap(entries:_*))

    LiftRules.statelessRewrite.append {

      case RewriteRequest(
      ParsePath(List("compound", action, id),_,_,_),_,_) =>
        RewriteResponse("compound" :: action :: Nil, Map("id" -> id))
      case RewriteRequest(
      ParsePath(List("method", action, id),_,_,_),_,_) =>
        RewriteResponse("method" :: action :: Nil, Map("id" -> id))
    }
  /*
    }
    LiftRules.rewrite.append {
      case RewriteRequest(
      ParsePath(List("compound", action, id),_,_,_),_,_) =>
        RewriteResponse("compound" :: action :: Nil, Map("id" -> id))
    }  */

    /*
     * Show the spinny image when an Ajax call starts
     */
    LiftRules.ajaxStart =
      Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)

    /*
     * Make the spinny image go away when it ends
     */
    LiftRules.ajaxEnd =
      Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)

   // LiftRules.statelessDispatchTable.append(WebServices) // stateless — no session created
   //  LiftRules.dispatch.append(WebServices)   // stateful

    LiftRules.early.append(makeUtf8)

    LiftRules.loggedInTest = Full(() => User.loggedIn_?)

    S.addAround(DB.buildLoanWrapper)
  }

  /**
   * Force the request to be UTF-8
   */
  private def makeUtf8(req: HTTPRequest) {
    req.setCharacterEncoding("UTF-8")
  }


}
